package com.qf.servlet;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/1/7 20:41
 * @Version 1.0
 * 展示所有用户
 */
@WebServlet("/user/list")
public class UserListServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");

        //进行权限拦截
        //获取用户的登录信息
        Object user = request.getSession().getAttribute("user");
        if (null==user){
            response.getWriter().write("<script>alert('您还未登录，请点击确定进行登录');location.href='" + request.getContextPath() + "/login.jsp';</script>");
            return;
        }
        //查询所有用户
        System.out.println("这里是查询所有用户的方法");
                //业务逻辑调用
        UserService userService=new UserServiceImpl();
        List<User> users=userService.showAllUser();
        //稍后转发给jspservlrt
        request.setAttribute("users",users);
        //通过转发进行数据传递
        request.getRequestDispatcher("/showAllUser.jsp").forward(request,response);

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}